package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.BoxCreateDto;
import com.example.front.dto.BoxDto;
import com.example.front.dto.BoxSearchDto;
import com.example.front.serivce.BoxService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "盒子管理")
@RestController
@CrossOrigin
@RequestMapping("/front/box")
public class BoxController {
    @Resource
    public BoxService boxService;

    @ApiOperation("添加盒子类型")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid BoxCreateDto boxCreateDto) {
        User me = UserContext.getUser();
        boxCreateDto.setTenantId(me.getTenantId());
        boxService.create(boxCreateDto);
        return new JsonResult<>();
    }

    @ApiOperation("列出盒子类型")
    @GetMapping("/list")
    public JsonResult<ListDto<BoxDto>> list(BoxSearchDto condition, Pagination pagination) {
        long startTime =  System.currentTimeMillis();
        condition.setTenantId(UserContext.getUser().getTenantId());
        ListDto<BoxDto> roleDtoList = boxService.list(condition, pagination);
        long endTime =  System.currentTimeMillis();
        long usedTime = (endTime-startTime)/1000;
        return new JsonResult<>(roleDtoList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid BoxCreateDto boxDto) {
        User me = UserContext.getUser();
        boxDto.setTenantId(me.getTenantId());
        boxService.update(boxDto);
        return new JsonResult<>();
    }

    @ApiOperation("删除盒子类型")
    @PostMapping("/delete")
    public JsonResult<String> deleteById(long id) {

        boxService.deleteById(id,UserContext.getUser().getTenantId());
        return new JsonResult<>();

    }
    @ApiOperation("列出盒子类型")
    @GetMapping("/listById")
    public JsonResult<ListDto<BoxDto>> listById(BoxSearchDto condition, Pagination pagination) {

        condition.setTenantId(UserContext.getUser().getTenantId());

        ListDto<BoxDto> roleDtoList = boxService.list(condition, pagination);

        return new JsonResult(roleDtoList.getList().get(0));
    }

}
